#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct node {
	int data;
	struct node * next;
}listnode, *linklist;

//元素入栈
void stack_push(linklist stack, int data);
//元素出栈
int stack_pop(linklist stack);
//创建栈
linklist creatStack();
//栈是否不为空
bool isNotEmpty(linklist stack);

int main(){

	//构建两个栈
	linklist stack1 = creatStack();
	linklist stack2 = creatStack();

	//往队列放元素
	for(int i = 1; i <= 10; i++){
		stack_push(stack1, i);
	}

	//从队列出元素
	while(1){
		if(isNotEmpty(stack2)){
			int data = stack_pop(stack2);
			printf(" %d", data);
		}else{
			if(isNotEmpty(stack1)){
				while(isNotEmpty(stack1)){
					stack_push(stack2,stack_pop(stack1));
				}
			}else{
				break;
			}
		}
	}

}


//元素入栈
void stack_push(linklist stack, int data){
	if(stack == NULL){
		perror("stack is NULL");
		return;
	}
	linklist node = (linklist)malloc(sizeof(listnode));
	if(node == NULL){
		perror("create node failed\n");
		return;
	}
	node->data = data;
	node->next = stack->next;
	stack->next = node;
}
//元素出栈
int stack_pop(linklist stack){
	linklist p;
	int data;
	if(stack == NULL){
		perror("stack is NULL");
		return -1;
	}
	if(isNotEmpty(stack)){
		p = stack->next;
		data = p->data;
		stack->next = p->next;
		free(p);
		return data;
	}else{
		return -1;
	}
	
}
//创建栈
linklist creatStack(){
	linklist head = (linklist)malloc(sizeof(listnode));
	if(head == NULL){
		perror("create node failed\n");
		return NULL;
	}
	head->next = NULL;
	head->data = 0;
	return head;
}
//栈是否不为空
bool isNotEmpty(linklist stack){
	if(stack == NULL){
		perror("stack is NULL");
		return false;
	}
	return stack->next != NULL;
}
